package com.citygis.transferdata.api.source2.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.citygis.transferdata.api.source2.dao.SecondMapper;
import com.citygis.transferdata.api.source2.entity.STcTaskMedia;
import com.citygis.transferdata.api.source2.entity.TcDetailMedia;
import com.citygis.transferdata.api.source2.entity.TcXcJgwbk;
import com.citygis.transferdata.api.source2.entity.WlTaskMedia;
import com.citygis.transferdata.api.source2.service.SecondService;
import com.citygis.transferdata.tools.DateUtil;
import com.citygis.transferdata.tools.InitSysLoad;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: SecondServiceImpl 
 * @Description: TODO
 * @author: liuzg
 * @Date: 2021/8/31 10:05
 * @Version: v1.0
 */
@Slf4j
@Service
public class SecondServiceImpl implements SecondService {

    @Resource
    private SecondMapper mapper;

    @Override
    public List<JSONObject> getList() {

        return mapper.getList();
    }

    @Override
    public String getTime() {
        return "当前时间为：" + DateUtil.getDate(0);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void DataPush(List<Map<String, Object>> data, String tableName) {
        if (null == data || data.size() == 0) {
            return;
        }
        int count = 0;
        for (Map<String, Object> map : data) {
            String sql = this.getInsertSql(map, tableName);
            int row = mapper.addData(sql);
            count += row;
        }
        log.info(tableName + "表添加数据量: " + count);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override // 添加媒体数据
    public void pushMedia(List<STcTaskMedia> data1, List<TcDetailMedia> data2, List<WlTaskMedia> data3, List<TcXcJgwbk> data4, String tableName) {
        int row = 0;
        int count = 0;
        if (null != data1) {
            for (STcTaskMedia sTcTaskMedia : data1) {
                row = mapper.addSTcTaskMedia(sTcTaskMedia);
                count += row;
            }
        }
        if (null != data2) {
            for (TcDetailMedia tcDetailMedia : data2) {
                row = mapper.addTcDetailMedia(tcDetailMedia);
                count += row;
            }
        }
        if (null != data3) {
            for (WlTaskMedia wlTaskMedia : data3) {
                row = mapper.addWlTaskMedia(wlTaskMedia);
                count += row;
            }
        }
        if (null != data4){
            for (TcXcJgwbk datum : data4) {
                row = mapper.addTcXcJgwbk(datum);
                count += row;
            }
        }
        log.info("媒体表：" + tableName + " 添加数据量: " + count);
    }

    // 拼接语句
    private String getInsertSql(Map<String, Object> data, String tableName) {
        //创建sql
        StringBuilder sql = new StringBuilder();
        //拼装插入的列和值
        Set<String> keySet = data.keySet();
        StringBuilder keys = new StringBuilder();
        StringBuilder values = new StringBuilder();
        int addFieldCount = 0;
        int KeySize = keySet.size();
        for (String key : keySet) {
            Object value = data.get(key);
            //如果值为空则不插入该字段
            if (null == value || "".equals(value)) {
                KeySize--;
                //解决后面的字段为空时出现多余逗号和少右括号问题
                if (KeySize == addFieldCount) {
                    keys.delete(keys.length() - 2, keys.length());
                    values.delete(values.length() - 3, values.length());
                    keys.append(")");
                    values.append(")");
                }
                continue;
            }
            if (addFieldCount == 0) {
                keys.append("(").append(key).append(", ");
                values.append("('").append(value).append("', '");
            } else if (addFieldCount < (KeySize - 1)) {
                keys.append(key).append(", ");
                values.append(value).append("', '");
            } else {
                keys.append(key).append(")");
                values.append(value).append("')");
            }
            addFieldCount++;
        }
        //拼装sql
        sql.append("INSERT INTO " + InitSysLoad.SOURCE2NAME + ".").append(tableName).append(" ");
        sql.append(keys).append(" values ").append(values);
        System.out.println("本次插入sql: " + sql);
        return sql.toString();
    }
}
